//
//  UserDetailVC.m
//  豆瓣
//
//  Created by dllo on 15/12/27.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "UserDetailVC.h"
#import "DataBase.h"
#import "ActivityModel.h"
#import "ActivityDetailsVC.h"
#import "HTDataBase_DB.h"
#import "Model_DB.h"

#define URL @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"


NSString *userDetai_cell_id = @"userDetai_cell_id";
@interface UserDetailVC ()

@property(nonatomic,strong)ActivityModel *model_detail;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *data_model;


@end

@implementation UserDetailVC



- (void)setData
{
    self.data_model = [NSMutableArray array];
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        
        
        
        
        //解析
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        for (NSDictionary *di in dic[@"events"]) {
            ActivityModel *model = [ActivityModel new];
            [model setValuesForKeysWithDictionary:di];
            
            [self.data_model addObject:model];
            
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            
        });
        
        
        
        
        
        
    }];
    
    [dataTask resume];
}




- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self setData];
//    [[DataBase shareDataBaseHandle] openDB];
//    NSMutableArray *array = [[DataBase shareDataBaseHandle] selectStudent];
//    if (array != nil) {
//        self.dataArray = array;
//    }
//    [[DataBase shareDataBaseHandle] closeDB];
    
  
  self.dataArray = [[HTDataBase_DB shareData] selectAllFromTable:@"Activity"];
    
    
    
    [self.tableView reloadData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
   //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:userDetai_cell_id];
    
    self.navigationItem.title = @"我的活动";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

  
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:userDetai_cell_id forIndexPath:indexPath];
    
    Model_DB *model = self.dataArray[indexPath.row];
 
    cell.textLabel.text = model.title;
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
//    activity_detailsVC.model_detail = self.dataArray[indexPath.row];
     Model_DB *model_db = self.dataArray[indexPath.row];
    for (ActivityModel *model in self.data_model) {
        if ([model.title isEqualToString:model_db.title]) {
            ActivityDetailsVC *activity_detailsVC = [[ActivityDetailsVC alloc] init];
            activity_detailsVC.model_detail = model;
            [self.navigationController pushViewController:activity_detailsVC animated:YES];
        }
    }
    
    
  
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这个方法，写这不用管就OK
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        

        
        
        
//        ActivityModel *model = self.dataArray[indexPath.row];
//        [[DataBase shareDataBaseHandle] openDB];
//        [[DataBase shareDataBaseHandle] deleteActivity:model.title];
//        [[DataBase shareDataBaseHandle] updataActivity:model];
//        [[DataBase shareDataBaseHandle] closeDB];
//        [self.dataArray removeObjectAtIndex:indexPath.row];
//        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
   
        Model_DB *model = self.dataArray[indexPath.row];
        [[HTDataBase_DB shareData] deleteModel:model Table:@"Activity"];
        [self.dataArray removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
      
        
    }];

       [self.tableView reloadData];
    return @[action1];
}








@end
